package com.jf.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jf.entity.Classes;
import com.jf.entity.Student;
import com.jf.mapper.ClassesMapper;
import com.jf.mapper.StudentMapper;
import com.jf.service.IStudentService;
import com.jf.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl implements IStudentService {
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private ClassesMapper classesMapper;

	/**
	 * 1. 查询所有学生
	 * @return
	 */
	@Override
	public List<Student> findAll() {
		List<Student> students = studentMapper.selectAll();
		for (Student student : students) {
			Integer cid = student.getCid();
			if (cid != null) {
				Classes classes = classesMapper.selectByPrimaryKey(cid);
				student.setCname(classes.getCname());
			}
		}
		return students;
	}

	/**
	 * 2. 分页查询
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@Override
	public PageResult<Student> findByPage(int page, int pageSize) {
		// 1. 开始分页
		PageHelper.startPage(page, pageSize);
		// 2. 查询所有记录
		List<Student> students = this.findAll();
		// 3. 将其转换为Page<Student>对象
		Page<Student> studentPage = (Page<Student>) students;
		// 4. 构造pageResult对象
		PageResult<Student> pageResult = new PageResult<>(studentPage.getTotal(), studentPage.getResult());
		// 5. 最后，返回
		return pageResult;
	}
}
